package com.caishi.lkx.exam.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.map.MapUtil;
import com.caishi.lkx.exam.mapper.PaperRuleMapper;
import com.caishi.lkx.exam.model.PaperRuleModel;
import com.caishi.lkx.exam.service.IPaperRuleService;
import com.caishi.lkx.exam.vo.SpecialConditionVo;
import com.zzw.common.entity.IBaseMapper;
import com.zzw.common.entity.impl.BSDAbsEntity;
import com.caishi.lkx.exam.ienum.type.QuestionType;

import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.LinkedList;
import java.util.List;

/**
 * @author by Janisa
 * @Description:
 * @date 2022/3/7 16:33
 */
@Service
@Slf4j
public class PaperRuleServiceImpl implements IPaperRuleService {

    @Resource
    private PaperRuleMapper paperRuleMapper;

    @Override
    public IBaseMapper<PaperRuleModel, String> getMapper() {
        return paperRuleMapper;
    }

    @Override
    public PaperRuleModel getCategoryRule(Long categoryId) {
        return selectFirst(wrappers()
                .apply("JSON_CONTAINS(categories, JSON_OBJECT('id', {0}))", categoryId)
                .orderByDesc(BSDAbsEntity::getModifyTime)
        );
    }

    @Override
    public List<SpecialConditionVo.TY> rule2vo(PaperRuleModel model) {
        var data = model.getContent();
        if (CollUtil.isEmpty(data)) return null;
        var m = data.get(0);
        List<SpecialConditionVo.TY> res = new LinkedList<>();
        var typeMap = MapUtil.builder("fiNum", QuestionType.fill)
                .put("juNum", QuestionType.judgment)
                .put("muNum", QuestionType.multipleChoice)
                .put("quNum", QuestionType.text)
                .put("siNum", QuestionType.singleChoice)
                .put("comNum", QuestionType.combination)
                .put("imNum", QuestionType.indefiniteMultipleChoice)
                .build();
        m.forEach((k, v) -> {
            var da = new SpecialConditionVo.TY();
            if (v instanceof Integer) {
                da.setCount((Integer) v);
            } else {
                da.setCount(0);
            }
            da.setType(typeMap.get(k));
            da.setName(da.getType().getDesc());
            res.add(da);
        });
        return res;
    }
}
